// scripts/debug-network.js
const hre = require("hardhat");

async function main() {
  console.log("🔍 X Layer 网络连接诊断");
  console.log("========================\n");

  // 1. 检查网络配置
  const networkName = hre.network.name;
  console.log(`使用的网络名称: ${networkName}`);

  const provider = hre.ethers.provider;
  
  // 2. 检查网络连接性
  try {
    const blockNumber = await provider.getBlockNumber();
    console.log(`✅ 网络连接成功 - 最新区块: ${blockNumber}`);
    
    const network = await provider.getNetwork();
    console.log(`✅ 网络信息 - ChainId: ${network.chainId}, Name: ${network.name}`);
    
  } catch (error) {
    console.error(`❌ 网络连接失败: ${error.message}`);
    return;
  }

  // 3. 检查目标地址
  const targetAddress = "0x7db543fe5ad961d3703b5ef3c61ef771e212d914";
  console.log(`\n检查地址: ${targetAddress}`);
  
  // 4. 检查余额
  try {
    const balance = await provider.getBalance(targetAddress);
    console.log(`余额 (wei): ${balance.toString()}`);
    console.log(`余额 (OKB): ${hre.ethers.formatEther(balance)}`);
    
    if (balance === 0n) {
      console.log("⚠️  余额为0 - 请从水龙头领取测试网OKB");
    }
  } catch (error) {
    console.error(`❌ 查询余额失败: ${error.message}`);
  }

  // 5. 检查随机一个有余额的地址作为对比
  console.log("\n🔁 对比测试: 检查随机地址余额");
  const randomAddress = "0x98245d0adf4595c66f0a9db8e13c44cbff6be459"; // 您之前提供的定序器地址
  try {
    const randomBalance = await provider.getBalance(randomAddress);
    console.log(`随机地址 ${randomAddress} 余额: ${hre.ethers.formatEther(randomBalance)} OKB`);
  } catch (error) {
    console.error(`对比测试失败: ${error.message}`);
  }
}

main().catch(error => {
  console.error("诊断脚本错误:", error);
  process.exit(1);
});